<?php
/**
 * @title 752. 打开转盘锁 - 层次遍历+Bit
 * @author start2004
 */

// ini_set("memory_limit", "128M");

class Solution {

    /**
     * @param String[] $deadends
     * @param String $target
     * @return Integer
     */
    function openLock($deadends, $target) {
        if($target == "0000"){
            return 0;
        } else {}

        /**
         * @since 2021-06-25 8位数组
         */
        $bit8Array = array_fill(0, 9999, "00000000");

        /**
         * @since 2021-06-25 哈希数组
         */
        $hashArray = [];

        /**
         * @since 2021-06-25 死亡数组
         */
        foreach ($deadends as $d){
            $hashArray[$d] = 1;
        }

        /**
         * @since 2021-06-25 死亡数组里有起始状态0000, Game Over
         */
        if(isset($hashArray["0000"])){
            return -1;
        } else {}

        /**
         * @since 2021-06-25 前进, 后退对应值, 避免if
         */
        $forwardArray = [
            "0" => "1",
            "1" => "2",
            "2" => "3",
            "3" => "4",
            "4" => "5",
            "5" => "6",
            "6" => "7",
            "7" => "8",
            "8" => "9",
            "9" => "0",
        ];
        $backArray = [
            "0" => "9",
            "1" => "0",
            "2" => "1",
            "3" => "2",
            "4" => "3",
            "5" => "4",
            "6" => "5",
            "7" => "6",
            "8" => "7",
            "9" => "8",
        ];

        /**
         * @since 2021-06-25 层次遍历
         */
        $dataArray = ["0000"];
        $n = 1;
        while($dataArray){
            /**
             * @since 2021-06-25 迭代
             */
            $nextArray = [];
            foreach ($dataArray as $d){
                $bit8 = $bit8Array[intval($d)];
                $nextBit = "10325476";
                $b = 0;

                /**
                 * @since 2021-06-25 逐位处理
                 */
                for($i=0; $i<=3; $i++){
                    if($bit8Array[$b] == "0"){
                        /**
                         * @since 2021-06-25 前进, +1
                         */
                        $s = $d;
                        $s[$i] = $forwardArray[$s[$i]];
                        if(!isset($hashArray[$s])){
                            $hashArray[$s] = 1;
                            $nextArray[] = $s;
                            $bit8Array[$s][$nextBit[$b]] = "1";
                        } else {}
                    } else {}
                    $b++;

                    /**
                     * @since 2021-06-25 后退, -1
                     */
                    if($bit8Array[$b] == "0"){
                        $s = $d;
                        $s[$i] = $backArray[$s[$i]];
                        if(!isset($hashArray[$s])){
                            $hashArray[$s] = 1;
                            $nextArray[] = $s;
                            $bit8Array[$s][$nextBit[$b]] = "1";
                        } else {}
                    } else {}
                    $b++;
                }
            }

            /**
             * @since 2021-06-25 是否目标值
             */
            if(isset($hashArray[$target])){
                return $n;
            } else {
                $dataArray = $nextArray;
            }
            $n++;
        }

        /**
         * @return
         */
        return -1;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202106/2021.06.25_4.php
 */
$datas = [
    [["0201","0101","0102","1212","2002"], "0202"], // 6
    [["8888"], "0009"], // 1
    [["8887","8889","8878","8898","8788","8988","7888","9888"], "8888"], // -1
    [["0000"], "8888"], // -1
    [["0001"], "0000"], // 0
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->openLock(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
